package com.test.auth_db_auto.controller;

import com.test.auth_db_auto.response.HttpResponse;
import com.test.auth_db_auto.response.SimpleResponse;
import com.test.auth_db_auto.support.BootSecurityProperties;
import com.test.auth_db_auto.support.DefaultBeanName;
import com.test.auth_db_auto.support.code.VerificationCode;
import com.test.auth_db_auto.support.code.VerificationCodeGeneration;
import com.test.auth_db_auto.support.code.picture.DefaultPictureCodeGeneration;
import com.test.auth_db_auto.support.code.sms.DefaultSmsCodeGenerator;
import com.test.auth_db_auto.support.properities.CodeStoreType;
import org.bouncycastle.util.encoders.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ Author: .Mr
 * @ ClassName CodeController
 * @ Description TODO
 * @ date 2021/12/25 17:29
 * @ Version 1.0
 * 获取验证码
 */
@Controller
@RequestMapping("${boot.oauth.code-path:/auth/code}")
public class CodeController {

    private BootSecurityProperties properties;
    private Map<String, VerificationCodeGeneration> codeGenerations;

    public CodeController(BootSecurityProperties properties, Map<String, VerificationCodeGeneration> codeGenerations) {
        this.properties = properties;
        this.codeGenerations = codeGenerations;
    }

    @RequestMapping()
    @ResponseBody
    public SimpleResponse<Map<String,Object>> codeGenerateJson(HttpServletRequest request, @RequestParam("type") String type) throws Exception {
        String tmp=type.trim();
        Map<String,Object> responseBody=new HashMap<>();
        if(tmp.equals("picture")){  // 获取图像的验证码
            String key = this.properties.getBaseLogin().getPictureCodeParameterName();
            if(this.properties.getCodeStoreType()== CodeStoreType.redis){
                key= UUID.randomUUID().toString();
            }

            VerificationCode<BufferedImage> pCode = ((DefaultPictureCodeGeneration)this.codeGenerations.get(DefaultBeanName.DEFAULT_PICTURE_CODE_GENERATOR_BEAN)).generator(key);

            ByteArrayOutputStream out = new ByteArrayOutputStream();

            ImageIO.write(pCode.getContent(),"png",out);

            byte[] bytes = out.toByteArray();

            String base64_code= Base64.toBase64String(bytes);  // 拿到生成的图像规则
            //删除 \n \r
            base64_code = base64_code.replaceAll("\n", "").replaceAll("\r", "");

            responseBody.put("value","data:image/png;base64,"+base64_code);
            responseBody.put("key",key);
        }else if(tmp.equals("sms")){  // 短信的验证码
            String key=request.getParameter(this.properties.getSmsLogin().getMobileParameterName());

            VerificationCode<String> smsCode = ((DefaultSmsCodeGenerator) this.codeGenerations.get(DefaultBeanName.DEFAULT_SMS_CODE_GENERATOR_BEAN)).generator(key);

            responseBody.put("value",smsCode.getContent());
            responseBody.put("key",key);
            responseBody.put("expire",smsCode.getExpirationTime()-20);
        }

        return HttpResponse.successSimpleResponse(responseBody);
    }
}
